package com.gl.saasservice.repository;

import com.gl.saasservice.entity.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface DepartmentRepository
        extends JpaRepository<Department,Long>, JpaSpecificationExecutor<Department> {

    List<Department> findByIdIn(List<Long> ids);

    boolean existsByParentId(Long parentId);

    Department findByCode(String  code);

    Department findByName(String  Name);

    Department findByParentId(Long parentId);

    Department findAllByParentIdAndName(Long parentId, String name);

    @Query(nativeQuery=true,value = "SELECT MAX(id) FROM `saas_department` WHERE parent_id = :parentId")
    Long findByParentIdMaxId(@Param("parentId") Long parentId);

    @Transactional
    void deleteByParentId(Long parentId);

}
